- Create a new file named Example.groovy
- Copy the file in the JIRA server, path {JIRA_HOME}/scripts/{package}/Example.groovy
- NOTE: if the script contains “package example.Example” {package} must be “example” in this case
- Then define the Custom Listener in JIRA > Add-ons > ScriptRunner Listeners
- In Note: put general Information, for example “MyFirstListener Example”
- In Project Key: put all the projects affected for this Listener when a issue is created
- Events: In this case, select “Issue Created”
- Name of groovy class: “example.Example”
And now, the script contents:
For example, this script obtain custom fields and then send a mail when a issue in a project is created
package example.Example
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import org.apache.log4j.Category
class TaskVersionListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskVersionListener.class)
@Override
void workflowEvent(IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by TaskVersionListener"
ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
MailServerManager mailServerManager = componentManager.getMailServerManager()
SMTPMailServer mailServer = mailServerManager.getDefaultSMTPMailServer()
MutableIssue myIssue = event.issue
CustomField cf_location = customFieldManager.getCustomFieldObject("customfield_11300")
CustomField cf_firstName = customFieldManager.getCustomFieldObject("customfield_12001")
CustomField cf_lastName = customFieldManager.getCustomFieldObject("customfield_12002")
CustomField cf_jobTitle = customFieldManager.getCustomFieldObject("customfield_12005")
CustomField cf_startDate = customFieldManager.getCustomFieldObject("customfield_12003")
CustomField cf_leaveDate = customFieldManager.getCustomFieldObject("customfield_12004")
CustomField cf_lineManager = customFieldManager.getCustomFieldObject("customfield_12007")
def location = myIssue.getCustomFieldValue(cf_location)
def firstName = myIssue.getCustomFieldValue(cf_firstName)
def lastName = myIssue.getCustomFieldValue(cf_lastName)
def jobTitle = myIssue.getCustomFieldValue(cf_jobTitle)
def lineManager = myIssue.getCustomFieldValue(cf_lineManager).displayName
def date = myIssue.getCustomFieldValue(cf_startDate)
if ( date == null ) {
date = myIssue.getCustomFieldValue(cf_leaveDate).format('dd/MM/yyyy')
} else {
date = myIssue.getCustomFieldValue(cf_startDate).format('dd/MM/yyyy')
}
if (mailServer) {
Email email = new Email("barcelona<raulpelaez@gmail.com>")
email.setSubject("New Starter: ${firstName} ${lastName} ${jobTitle} ${date}" )
String content = "Hello ${lineManager},"
content += "<br>"
content += "<br>"
content += "We are pleased to confirm that ${firstName} ${lastName} ${jobTitle} will start employment on ${date}."
content += "<br>"
content += "<br>"
content += "Regards,"
email.setMimeType("text/html")
email.setBody(content)
mailServer.send(email)
}
else {
log.error "mailServer: No SMTP mail server defined"
}
}
}
Then when an issue is created in the project target, you can see in the “atlassian-jira.log” the traces:
2015-05-22 11:25:52,860 ajp-bio-8009-exec-46 DEBUG sysadmin 685x25762x1 ddtuzy 213.229.153.36 /secure/QuickCreateIssue.jspa [example.Example.TaskVersionListener] Event: 1 fired for SL-111 and caught by TaskVersionListener





